/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ces31.ChineseDicesServer.IP;

import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Imada
 */
public class IP_Ranking {

    class RankingEntry implements Comparable<RankingEntry> {

        private String player;
        private Integer points;

        public RankingEntry(String player, Integer points) {
            this.player = player;
            this.points = points;
        }

        public String getPlayer() {
            return player;
        }

        public Integer getPoints() {
            return points;
        }

        public void increasePoints() {
            this.points = this.points + 1;
        }

        public int compareTo(RankingEntry o) {
            //order by the the highest value
            return (-1 * points.compareTo(o.points));
        }
    }

    private IP_Ranking(List<RankingEntry> ranking){
        this.ranking = ranking;
    }

    private IP_Ranking() {
        loadRanking();
    }

    public static IP_Ranking getInstance() {
        if (rankingInstance == null) {
            rankingInstance = new IP_Ranking();
        }
        return rankingInstance;
    }

    public void loadRanking() {
        if (persist) {
            try {
                XStream xstream = new XStream();
                File rankingDB = new File("ranking.xml");
                //File rankingDB = new File(Class.class.getResource("/ranking.xml").getFile());
                String data = FileUtils.readFileToString(rankingDB);
                ranking = (List<RankingEntry>) xstream.fromXML(data);
            } catch (Exception e) {
                System.err.println("Error loading ranking from ranking.xml: Couldn't read file");
                //e.printStackTrace();
            }
        }
    }

    public void saveRanking() {
        if (persist) {
            try{
                XStream xstream = new XStream();
                File rankingDB = new File("ranking.xml");
                String data = xstream.toXML(ranking);
                FileUtils.writeStringToFile(rankingDB, data);
            } catch(Exception e){
                System.err.println("Error updating ranking.xml: Couldn't write to file");
                //e.printStackTrace();
            }
        }
    }

    public void atualizarRanking(String winnerName) {
        if (findPlayer(winnerName) == -1) {
            ranking.add(new RankingEntry(winnerName, 0));
        }
        ranking.get(findPlayer(winnerName)).increasePoints();
        Collections.sort(ranking);
        saveRanking();
    }

    public Integer getRanking(String nome) {
        return findPlayer(nome) + 1;
    }

    private Integer findPlayer(String nome) {
        Integer index = 0;
        Iterator<RankingEntry> iterator = ranking.iterator();
        while (iterator.hasNext()) {
            RankingEntry player = iterator.next();
            if (nome.equals(player.getPlayer())) {
                return index;
            }
            index = index + 1;
        }
        return -1;
    }
    private List<RankingEntry> ranking = new LinkedList<RankingEntry>();
    private static IP_Ranking rankingInstance;
    private static boolean persist = true;

    //methods for tests
    /**
     * Tirar o public desse método!!! É só temporário!
     */
    static void disablePersistance() {
        persist = false;
    }

    static void destroy() {
        rankingInstance = null;
    }
}